<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Console\Commands;

use App\Models\PointOrder;
use App\Models\ProductOrder;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class CloseExpiredOrders extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'CloseExpired:Orders';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '自动关闭未支付订单';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->CloseProductOrder();
        $this->ClosePointOrder();
    }

    public function CloseProductOrder()
    {
        $expiredOrders = ProductOrder::query()->where('status', 1)
            ->where('created_at', '<=', now()->subMinutes(30))
            ->get();

        foreach ($expiredOrders as $order) {
            DB::transaction(function () use ($order) {
                // 关闭订单
                $order->delete();
            });
        }
    }

    public function ClosePointOrder()
    {
        $expiredOrders = PointOrder::query()->where('status', 1)
            ->where('created_at', '<=', now()->subMinutes(30))
            ->get();

        foreach ($expiredOrders as $order) {
            DB::transaction(function () use ($order) {
                // 关闭订单
                $order->delete();
            });
        }
    }
}
